package mid

import (
	"github.com/gin-gonic/gin"
	nhttp "net/http"
	"wzgames/internal/admin/adc"
	"wzgames/internal/admin/adcomm"
	"wzgames/internal/admin/model"
	"wzgames/internal/response"
)

func WZAuth() gin.HandlerFunc {
	return func(c *gin.Context) {
		token := c.Request.Header.Get(adcomm.AdminTokenKey)
		if token == "" {
			c.JSON(nhttp.StatusOK, model.Response{Code: response.ErrUnAuthorize, Message: "UnAuthorize"})
			c.Abort()
			return
		}
		adminId, code := adc.PP().CheckTicket(token)
		if code != 0 {
			c.JSON(nhttp.StatusOK, model.Response{Code: code, Message: "UnAuthorize"})
			c.Abort()
			return
		}
		c.Set(adcomm.AdminIdKey, adminId)
		c.Next()
	}
}

func AdminId(c *gin.Context) int64 {
	v, exists := c.Get(adcomm.AdminIdKey)
	if !exists {
		return 0
	}
	adminId, ok := v.(int64)
	if ok {
		return adminId
	}
	return 0
}
